package com.ludaxin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ludaxin.dao.UserDAO;
import com.ludaxin.model.Account;
import com.ludaxin.model.Json;
import com.ludaxin.model.Page;
import com.ludaxin.model.vo.UserVo;

@Controller
public class UserController {
    @Resource(name = "userDAO")
    private UserDAO userDAO;

  /*  @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request,
	    HttpServletResponse response, UserVo user, ModelMap modelMap) {
	List list = userDAO.getUsers(user);
	modelMap.addAttribute("users", list);
	return new ModelAndView("index", modelMap);
    }*/
    @InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Json login(HttpServletRequest request,
	    HttpServletResponse response, UserVo user) {
	List list = userDAO.getUsers(user);
	Json j = new Json();
	if(list.size()>0){
	    j.setSuccess(true);
	    j.setMsg("登录成功！");
	    j.setObj(list);
	}else {
		j.setMsg("登录失败！");
	}
	return j;
    }
    @RequestMapping(value = "/userList")
    @ResponseBody
    public Json userList(HttpServletRequest request,
	    HttpServletResponse response, Account account) {
    	System.out.println("par:"+request.getParameter("userName"));
    	String whereStr = request.getParameter("userName");
	String pageNowS=request.getParameter("currentPage")==null?"1":request.getParameter("currentPage");
	//pageNowS第几页
	int currentPage=Integer.parseInt(pageNowS);
	
	String pageSizes=request.getParameter("pageSize")==null?"1":request.getParameter("pageSize");
	//pageSizeS每页多少条数据
	int pageSize=Integer.parseInt(pageSizes);
	
	//currentResult第几条数据，从0开始
	int currentResult=0;
	if(currentPage>1){
		currentResult=(currentPage-1)*pageSize;
	}		
	
	Page page=new Page();
	page.setCurrentResult(currentResult);
	page.setPageSize(pageSize);
	page.setCurrentPage(currentPage);
	List list=null;int count=0;
	if(whereStr !=null){
		count=userDAO.getAllAccountsCount(whereStr);
		list = userDAO.getAllAccountsPager(account, page,whereStr);
	}else{
		count = userDAO.getAllAccountsCount();
		list = userDAO.getAllAccountsPager(account, page);
	}
	
	
	int totalPage=count%pageSize==0?(count/pageSize):(count/pageSize+1);
	page.setTotalPage(totalPage);
	Map<String,Object> resultMap=new HashMap<String, Object>();
	resultMap.put("page", page);
	resultMap.put("list", list);
	Json j = new Json();
	if(list.size()>0){
	    j.setSuccess(true);
	    j.setMsg("登录成功！");
	    j.setObj(resultMap);
	}else {
		j.setMsg("登录失败！");
	}
	return j;
    }
    public UserDAO getUserDAO() {
	return userDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
	this.userDAO = userDAO;
    }
}
